package com.better517na.elasticseach.handlers;



import com.better517na.elasticseach.elasticsearch.Index;
import com.better517na.elasticseach.elasticsearch.client.ESHttpClient;
import com.better517na.elasticseach.utils.ESUtils;

import java.util.Map;

public class CustomExportHandler extends AbstractHandler implements Handler {

    private static final String META_DATA_TEMPLATE = "'{'\"index\":'{'\"_index\":\"{0}\",\"_type\":\"{1}\",\"_id\":\"{2}\"'}}'";

    public void doWork() {
        ESHttpClient producer = new ESHttpClient(context.getEsHost(), context.getEsPort());

        Index index = context.getProducer();
        boolean producerStatus = producer.indexStatus(index);

        if (!producerStatus) {
            throw new RuntimeException("未获取到索引信息");
        }
        index.setAction("_search");

        try {
            String queryBody = context.getCustom().getQueryDsl();
            String result = producer.post(index, queryBody);
            Map<String, String> _docs = ESUtils.getHitsWithID(result);
            exportData(constructContent(_docs, META_DATA_TEMPLATE), 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
